package com.example.demo1.controller;

import com.example.demo1.entity.BusinessMsgEnum;
import com.example.demo1.entity.JsonResult;
import com.example.demo1.exception.BusinessErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhw
 */

@RestController
@RequestMapping("/exception")
public class ExceptionController {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionController.class);

    @PostMapping("/test")
    public JsonResult test(@RequestParam("name") String name,
                           @RequestParam("pass") String pass) {
        logger.info("name：{}", name);
        System.out.println("name:" + name);
        logger.info("pass：{}", pass);

        return new JsonResult();
    }

    @RequestMapping("/null")
    public JsonResult testNull() {
        Date date = null;
        long time = date.getTime();
        return new JsonResult();
    }

    @RequestMapping("array")
    public JsonResult testArray() {
        int[] arr = {1, 2, 3};
        int i = arr[3];
        logger.info("i");
        return new JsonResult();
    }

    @RequestMapping("/add1")
    public JsonResult add1() {
        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String format = simpleDateFormat.format(date);
        System.out.println(format);
        try {
            Date parse = simpleDateFormat.parse("2020-1212");
            System.out.println(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("=================");
        return new JsonResult();
    }

    @RequestMapping("/add2")
    public JsonResult add2() throws ParseException {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        System.out.println(format);
        Date parse = simpleDateFormat.parse("2020-1212");
        System.out.println(parse);
        System.out.println("=================");
        return new JsonResult();
    }


    @RequestMapping("/custom")
    public JsonResult custom() {
        try{
            int i=1/0;
        }catch (Exception e){
            throw new BusinessErrorException(BusinessMsgEnum.UNEXPECTED_EXCEPTION);
        }
        return new JsonResult();
    }

    @RequestMapping("/custom2")
    public JsonResult custom2(){
        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String format = simpleDateFormat.format(date);
        System.out.println(format);
        try {
            Date parse = simpleDateFormat.parse("2020-1212");
            System.out.println(parse);
        } catch (Exception e) {
            throw  new BusinessErrorException(BusinessMsgEnum.DATA_EXCEPTION);
        }
        System.out.println("=================");

        BusinessErrorException businessErrorException = new BusinessErrorException(BusinessMsgEnum.PARMETER_EXCEPTION);
        return new JsonResult();
    }



}
